import axios from "axios";
export const ROOT_URL = process.env.NODE_ENV === "development"
    ? "/cpasApi"
    : localStorage.getItem("cpasApiUrl");

export const ROOT_URL_TE = process.env.NODE_ENV === "development"
    ? "/cpasTeApi"
    : localStorage.getItem("cpasApiUrl");


export default class HttpClient {
    static instance;
    RootUrl = ROOT_URL;
    constructor() { }
    static getInstance() {
        if (!this.instance) {
            this.instance = new HttpClient();
        }
        return this.instance;
    }
    //todo 完整参数封装
    async fetchGet(url) {
        try {
            const response = await fetch(this.RootUrl + url);
            return response.json();
        }
        catch (e) {
            throw e;
        }
    }
    async fetchPost(url, params) {
        try {
            const response = await fetch(this.RootUrl + url, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify(params),
            });
            return response.json();
        }
        catch (e) {
            throw e;
        }
    }
}
/**
 * 获取 sql 模板接口数据
 * @param apiName api名称
 * @param params 发送参数，一般都为 JSON格式
 * @returns ServerResult
 */
export async function getApiData(apiName, params) {
    const pd = {
        name: apiName,
    };
    if (params) {
        pd.params = params;
    }
    try {
        const res = await HttpClient.getInstance().fetchPost(`/sqlapis/getApiData`, pd);
        if (res.code !== 0) {
            // console.log(`请求返回错误,code为：${res.code}`);
        }
        return res;
    }
    catch (error) {
        return Promise.resolve({ data: error, ok: false, code: 100000 });
    }
}
/**
 * 获取 nest 接口数据
 * @param apiURL api请求接口地址
 * @param apiMethod api请求方式
 * @param params 发送参数，一般都为 JSON格式
 * @returns ServerResult
 */
export async function getNestApiData(apiURL, apiMethod, params) {
    const apiMethodTo = apiMethod.toUpperCase();
    if (apiMethodTo === "POST") {
        const response = await fetch(`/cpasApi${apiURL}`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(params),
        });
        return response.json();
    }
    if (params) {
        let paramsArray = [];
        //拼接参数
        Object.keys(params).forEach((key) => paramsArray.push(key + "=" + params[key]));
        if (apiURL.search(/\?/) === -1) {
            apiURL += "?" + paramsArray.join("&");
        }
        else {
            apiURL += "&" + paramsArray.join("&");
        }
    }
    console.log(apiURL, "@@@");
    const response = await fetch(`/cpasApi${apiURL}`);
    return response.json();
}
/**
 * 获取 nest 接口数据
 * @param apiURL api请求接口地址
 * @param apiMethod api请求方式
 * @param params 发送参数，一般都为 JSON格式
 * @returns ServerResult
 */
export async function getOnelineApiData(apiName, params) {
    const pd = {
        name: apiName,
    };
    if (params) {
        pd.params = params;
    }
    const response = await fetch(`/cloudServerApi/sqlapis/getApiData`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
        },
        body: JSON.stringify(pd),
    });
    return response.json();
}
export async function axiosGet(url, params, headers) {
    return await axios.get(ROOT_URL + url, {
        params,
        headers,
    });
}
export async function axiosPost(url, params) {
    return await axios.post(ROOT_URL + url, params);
}
export async function axiosDelete(url, params, headers) {
    return await axios.delete(ROOT_URL + url, {
        data: params,
        headers,
    });
}
export async function axiosPut(url, params, headers) {
    return await axios.put(ROOT_URL + url, params, headers);
}
